<?php

namespace backend\services\goods;

use backend\extensions\ApiBaseService;
use backend\models\goods\goods\Goods;
use backend\models\goods\cate\GoodsCate;
/**
 * Desc 商品管理服务类
 * @author WMX
 */
class GoodsService extends ApiBaseService
{

    /**
     * 列表
     * @param array $params 参数 
     * **/
    public static function getList($params)
    {
        // 检索条件
        $condition = ['AND'];
        if ($params['barcode']) {
            $condition[] = ['=', 'barcode', $params['barcode']];
        }
        if ($params['name']) {
            $condition[] = ['like', 'name', $params['name']];
        }
        if ($params['category']) {
            $condition[] = ['=', 'category', $params['category']];
        }
        if ($params['state']) {
            $condition[] = ['=', 'state', $params['state']];
        }
        $list = Goods::getDataAll($condition, $params['page'], $params['page_size']);

        if (empty($list['list'])) return $list;

        $cate_list = GoodsCate::getIndexAll('id');

        foreach($list['list'] as &$item) {
            if (!empty($item['img']) && is_string($item['img'])) {
                $imgs = json_decode($item['img'], true);
                $item['img'] = $imgs;
            } else {
                $item['img'] = [];
            }
            $item['cate_info'] = $cate_list[$item['category']] ?? [];
            $item['cate_name'] = $cate_list[$item['category']]['name'] ?? '';
        }

        return $list;

    }

    /**
     * 新增
     * @param array $params 参数 
     * * */
    public static function create($params)
    {
        $transaction=\Yii::$app->db->beginTransaction();
        try {
            $params['spec'] = isset($params['spec']) && !empty($params['spec']) ? json_encode($params['spec']) : json_encode([]);
            $params['barcode'] = Goods::generateUniqueBarcode();
            $model_id = Goods::create($params);
            $transaction->commit();
            return self::jsonSuccess($params);
        } catch (\Exception $e) {
            $transaction->rollBack();
            return self::jsonError($e->getMessage());
        }
       
    }

    /**
     * 新增
     * @param array $params 参数 
     * * */
    public static function update($params)
    {
        $condition = ['id' => $params['id']];
        unset($params['id']);
        $params['spec'] = isset($params['spec']) && !empty($params['spec']) ? json_encode($params['spec']) : json_encode([]);
        if (Goods::updateData($params, $condition) === false) {
            return self::jsonError('更新失败');
        }
        return self::jsonSuccess();
    }

    /**
     * 删除
     * @param array $params 参数 
     * * */
    public static function delete($params)
    {
        $condition = ['id' => $params['id']];
        if (Goods::del($condition) == false) {
            return self::jsonError('删除失败');
        }
        return self::jsonSuccess();
    }

    /**
     * 详情
     * @param array $params 参数 
     * **/
    public static function detail($params)
    {
        $condition = ['id' => $params['id']];
        $data = Goods::getDataOne($condition, '', []);
        $data['spec'] = !empty($data['spec']) ? json_decode($data['spec']): [];
        return self::jsonSuccess($data);
    }

    /**
     * 更新
     * @param array $params 参数 
     * * */
    public static function colUpdate($params)
    {
        $condition = ['in','id', $params['id']];
        unset($params['id']);
        if (Goods::updateData($params, $condition) === false) {
            return self::jsonError('更新失败');
        }
        return self::jsonSuccess();
    }

}
